/**
 * 文件名：BaseBean.java
 *
 * 版本信息：
 * 日期：2009-6-17
 * Copyright dp Corporation 2009 
 * 版权所有 2009All rights reserved.
 * 创建者：wht
 *
 */

package com.dp.pub;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.dp.util.DataMap;


/**
 * 
 * 项目名称：book
 * 类名称：BaseBean
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2009-6-17 下午02:06:10
 * 修改人：Administrator
 * 修改时间：2009-6-17 下午02:06:10
 * 修改备注：
 * @version 
 * 
 */

public class BaseBean {
	//protected Session ss = null;	
//	protected Connection conn=null;
//    protected Statement stmt=null;
//	protected PreparedStatement prep=null;
//    protected PreparedStatement prep1=null;
//    protected PreparedStatement prep2=null;        
//	protected CallableStatement cstmt=null;
//	protected ResultSet rs1=null;
//	protected ResultSet rs2=null;
//	protected ResultSet rs3=null;   
//        protected PrintWriter out=null;
        protected String sql;
        protected Logger logger = Logger.getLogger(this.getClass()); 
       /* protected EntityManager em;
        protected void setEntityManager(EntityManager em) {
            this.em = em;
        }        
        protected void initEM() 
        {                    
        	em=HUtil.getEMF().createEntityManager();                                      
        }   
        protected EntityManager getEntityManager() {
            return em;
        }*/
        
 
//       protected Connection getConn() {
//    	  conn= HUtil.getConn();
//
//    	  return conn;
//	}
//          protected Connection getConn(String dsjndi) {
//              Connection conn1=null;
//		
//		try {
//			InitialContext ctx = new InitialContext();
//			DataSource ds = (DataSource) ctx.lookup(dsjndi);
//			conn1 = ds.getConnection();
//			conn1.setAutoCommit(false);
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//		return conn1;
//	}
//         protected void init() throws Exception
//        {                    
//
//        	 conn=getConn();
//             stmt=conn.createStatement();                                       
//        }
//         
//         protected void initRptConnection() throws Exception
//         {                    
//             // ss=getsession();
//             // conn=ss.connection();
//         	   conn=HUtil.getRptMConn();
//              stmt=conn.createStatement();                                       
//         }
//         
//       	protected void closeAll() //throws Exception
//	{
//		try{
//				
//			 if(rs1!=null){ rs1.close();rs1=null;}
//			 if(rs2!=null){rs2.close();rs2=null;}
//			 if(rs3!=null){rs3.close();rs3=null;}
//			 if(prep!=null){prep.close();prep=null;}
//             if(prep1!=null){prep1.close();prep1=null;}
//             if(prep2!=null){prep2.close();prep2=null;}
//             if(cstmt!=null){cstmt.close();cstmt=null;}
//			 if(conn!=null && !conn.isClosed()){
////				 conn.setAutoCommit(false);
////				 conn.commit();
////				 conn.close();
//				 
//					//conn =org.springframework.jdbc.datasource.DataSourceUtils.getConnection((DataSource) MyServer.ctx.getBean("dataSource"));
//					
//					org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection(conn, (DataSource) MyServer.ctx.getBean("dataSource"));
//				 
//				 
//				 conn=null;
//			 }
//			 else if(conn!=null){
//				 conn=null;
//			 }
//		       //  if(ss!=null){ss.flush();if(ss.isOpen())ss.close();ss=null;}
//             if(out!=null){out.close();out=null;}
//		}
//		catch(Exception e)
//		{ 
//			rs1=null;
//			rs2=null;
//			rs3=null;
//			prep=null;
//            prep1=null;
//            prep2=null;
//            cstmt=null;
//			conn=null;
//			// ss = null;
//            out=null;
//            e.printStackTrace();
//			//throw e;
//		}
//		
//	}
        public static boolean StringIsNull(Object str) {
            boolean b = false;
            try {
                if (!str.equals("") && !str.equals("null")) {
                    b = true;
                }
            } catch (Exception e) {
            }
            return b;
        }
        public static boolean StringIsNotNull(Object str) {
            boolean b = false;
            try {
                if (!str.equals("") && !str.equals("null")) {
                    b = true;
                }
            } catch (Exception e) {
            }
            return b;
        }
        public static String substring(String string, int i, int j) {
            try {
                return string.substring(i, j);
            } catch (Exception e) {
                return "";
            }
        }
        public static String substring(String string, String mark, int i) {
            try {
                return string.substring(string.indexOf(mark), i);
            } catch (Exception e) {
                return "";
            }
        }
        public static String substring(String string, String mark1, String mark2) {
            try {
                return string.substring(string.indexOf(mark1), string.indexOf(mark2));
            } catch (Exception e) {
                return "";
            }
        }
        public static String substring(String string, int i, String mark) {
            try {
                return string.substring(i, string.indexOf(mark));
            } catch (Exception e) {
                return "";
            }
        }
        public static String ObjectToStringDB(Object obj) {
            try {
                String str = obj.toString().trim();

                if (str.equals("") || str.equals("null")) {
                    return "null";
                }

                return "'" + str + "'";
            } catch (Exception e) {
                return "null";
            }
        }
        public static String ObjectToString(Object obj) {
            try {
                return obj.toString().trim();
            } catch (Exception e) {
                return "";
            }
        }
        public static int getint(Object value) {
            try {
                return Integer.parseInt(ObjectToString(value));
            } catch (Exception e) {
                return 0;
            }
        }
        public static int getint(Object value, int radix) {
            try {
                return Integer.parseInt(ObjectToString(value), radix);
            } catch (Exception e) {
                return 0;
            }
        }
        public static long getlong(Object value) {
            try {
                return Long.parseLong(ObjectToString(value));
            } catch (Exception e) {
                return 0;
            }
        }
        public static float getfloat(Object value) {
            try {
                return Float.parseFloat(ObjectToString(value));
            } catch (Exception e) {
                return 0f;
            }
        }
        public static double getdouble(Object value) {
            try {
                return Double.parseDouble(ObjectToString(value));
            } catch (Exception e) {
                return 0.0;
            }
        }
        public static String getName(String s) {
            s = s.substring(0, 1).toUpperCase() +
                s.substring(1, s.length()).toLowerCase();

            return s;
        }
        public static String ChangeString(String value, String charset) {
            try {
                value = ObjectToString(value);
                value = new String(value.getBytes(charset));
            } catch (Exception e) {
                e.getMessage();
            }

            return value;
        }
        public static String getNowTime(String timeFormat) {
            SimpleDateFormat lformat = new SimpleDateFormat(timeFormat);
            Calendar now = Calendar.getInstance();
            String nowstr = lformat.format(now.getTime());

            return nowstr;
        }
        public static String getNowTime() {
            SimpleDateFormat lformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar now = Calendar.getInstance();        
    		now.setTime(now.getTime());
            String nowstr = lformat.format(now.getTime());

            return nowstr;
        }
        public static String getStrDate(Date date) {
            SimpleDateFormat lformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datestr = lformat.format(date);
            return datestr;
        }
        public static void  main(String a[])
        {
        
        	
      	
    		Calendar   cal     =   Calendar.getInstance();   
      		Date date=new Date();
      		date.setHours(0);
      		date.setMinutes(0);
      		date.setSeconds(0);
      		cal.setTime(date);   
      		cal.add(GregorianCalendar.MONTH,1); 
      		
      		int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(cal.getTime()));
    		int month=Integer.parseInt(new SimpleDateFormat("MM").format(cal.getTime()));
    		int day=Integer.parseInt(new SimpleDateFormat("dd").format(cal.getTime()));
    		
    		
    		//cal.set(year, month, date, 0, 0, 0);
      		
      		SimpleDateFormat   theDate   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
      		String   dateString   =   theDate.format(cal.getTime());   
      		
        }
        public static DataMap getAttributeNames(HttpSession session) {
            DataMap dataMap = new DataMap();
            Enumeration enumeration = session.getAttributeNames();
            String object = null;
            while (enumeration.hasMoreElements()) {
                object = BaseBean.ObjectToString(enumeration.nextElement());
                dataMap.put(object, session.getAttribute(object));
            }
            return dataMap;
        }
        public static DataMap getAttributeNames(DataMap dataMap,HttpSession session) {       
            Enumeration enumeration = session.getAttributeNames();
            String object = null;
            while (enumeration.hasMoreElements()) {
                object = BaseBean.ObjectToString(enumeration.nextElement());
                dataMap.put(object, session.getAttribute(object));
            }
            return dataMap;
        }
    	public static String encode(String string) {
            StringBuffer result = new StringBuffer();
            int i;

            for (i = 0; i < string.length(); i++) 
            {
               if(" ".equals(""+string.charAt(i)))
               {
               		result.append("%20");
               		continue;
               }
               
               else if("������������*������������������������������".indexOf(""+string.charAt(i))!=-1)//���ı�� ��֪��ȡֵ��Χ ��ʱ����
               {
               	
               		result.append(java.net.URLEncoder.encode(""+string.charAt(i)).toLowerCase());
               		continue;
               }
                if (string.charAt(i) >= 0x4e00) {
                   
                    String hex = ""+string.charAt(i);                
                    hex=java.net.URLEncoder.encode(hex);
                    hex=hex.toLowerCase();
                    result.append(hex);
                } else 
                {
                	result.append(string.charAt(i));                
                    
                }
            }
    		
            return result.toString();
        }
    
}
